package com.hskn.hss.module.grid.vo;

import com.hskn.hss.utils.StringUtils;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Data
public class GridPlace implements Serializable {
    private static double PI = 3.14159265;
    private static double EARTH_RADIUS = 6378137;
    private static double RAD = Math.PI / 180.0;
    /**
     * 经度x
     */
    private String lng;
    /**
     * 纬度y
     */
    private String lat;
    /**
     * 距离
     */
    private Double distance;

    public GridPlace(String lng, String lat) {
        this.lng = lng;
        this.lat = lat;
    }

    public GridPlace(Double lng, Double lat) {
        this.lng = String.valueOf(lng);
        this.lat = String.valueOf(lat);
    }

    public GridPlace(String lng, String lat, Double distance) {
        this.lng = lng;
        this.lat = lat;
        this.distance = distance;
    }

    public GridPlace() {
    }

    public Double getDoubleLng() {
        if (StringUtils.isNotEmpty(lng)) {
            return Double.valueOf(lng);
        }
        return null;
    }

    public Double getDoubleLat() {
        if (StringUtils.isNotEmpty(lat)) {
            return Double.valueOf(lat);
        }
        return null;
    }

    /**
     * 取两点的中点
     *
     * @param lng
     * @param lat
     * @param lng1
     * @param lat1
     * @return
     */
    public static GridPlace buildGridPlace(Double lng, Double lat, Double lng1, Double lat1) {
        Double resultLng = (lng + lng1) / 2;
        Double resultLat = (lat + lat1) / 2;
        return new GridPlace(String.valueOf(resultLng), String.valueOf(resultLat));
    }

    /**
     * 获取圆的外切正方形的四个角的坐标
     *
     * @param lng    圆心经度
     * @param lat    圆心纬度
     * @param radius 半径
     * @return
     */
    public static List<GridPlace> buildPointsCircular(Double lng, Double lat, Double radius) {
        List<GridPlace> resultList = new ArrayList<>();
        Double latitude = lat;
        Double longitude = lng;

        Double degree = (24901 * 1609) / 360.0;
        double raidusMile = radius;

        Double dpmLat = 1 / degree;
        Double radiusLat = dpmLat * raidusMile;
        Double minLat = latitude - radiusLat;
        Double maxLat = latitude + radiusLat;

        Double mpdLng = degree * Math.cos(latitude * (PI / 180));
        Double dpmLng = 1 / mpdLng;
        Double radiusLng = dpmLng * raidusMile;
        Double minLng = longitude - radiusLng;
        Double maxLng = longitude + radiusLng;
        resultList.add(new GridPlace(maxLng, maxLat));
        resultList.add(new GridPlace(maxLng, minLat));
        resultList.add(new GridPlace(minLng, maxLat));
        resultList.add(new GridPlace(minLng, minLat));
        return resultList;
    }

    /**
     * 获取多边形的外切正方形的四个角的坐标
     * @param gridPlaceList
     * @return
     */
    public static List<GridPlace> buildPointsPolygon(List<GridPlace> gridPlaceList){
        List<GridPlace> resultList = new ArrayList<>();
        Double minLng = gridPlaceList.stream().map(GridPlace::getDoubleLng).min((x1,x2)->{
            return x1.compareTo(x2);
        }).get();

        Double maxLng = gridPlaceList.stream().map(GridPlace::getDoubleLng).max((x1,x2)->{
            return x1.compareTo(x2);
        }).get();
        Double minLat = gridPlaceList.stream().map(GridPlace::getDoubleLat).min((x1,x2)->{
            return x1.compareTo(x2);
        }).get();
        Double maxLat = gridPlaceList.stream().map(GridPlace::getDoubleLat).max((x1,x2)->{
            return x1.compareTo(x2);
        }).get();
        resultList.add(new GridPlace(maxLng, maxLat));
        resultList.add(new GridPlace(maxLng, minLat));
        resultList.add(new GridPlace(minLng, maxLat));
        resultList.add(new GridPlace(minLng, minLat));
        return resultList;
    }
    public static void main(String[] args) {
        List<GridPlace> placeList =
                buildPointsCircular(119.12353, 36.71353, 94.72);
        int i = 1;
    }

}